<template>
	<view class="content">
		<!-- 导航栏 -->
		<u-navbar title-color="#fff" back-icon-color="#ffffff" is-back="true" :background="background" :back-text-style="{ color: '#fff' }"
		 title="设备管理" back-icon-name="nav-back">
			<view class="navbar-right" slot="right">
				<view class="dot-box right-item fo-white paddlr20">
					<!-- <text class="cuIcon-refresh fo-46 mr20" @click="AsyncData"></text> -->
					<!-- <text class="cuIcon-roundadd fo-46" @click="onAdd"></text> -->
				</view>
			</view>
		</u-navbar>
		<button @click="onBeep">发出3次蜂鸣声</button>
		<button @click="onVibrate">设备震动2S</button>
		<button @click="onGetDecive">获取设备信息</button>
		<view style="display: flex;">
			<button style="flex:1" @click="onGetVolume">获取设备系统音量</button>
			<button @click="SetVolume('add')">音量+1</button>
			<button @click="SetVolume('Sub')">音量-1</button>
		</view>
		<view style="display: flex;">
			<button style="flex:1" @click="getBrightness">获取屏幕亮度</button>
			<button @click="SetBrightness('add')">亮度+1</button>
			<button @click="SetBrightness('sub')">亮度-1</button>
		</view>
		<view style="display: flex; align-items: center;">
			<text style="flex:1;text-align: center;">锁</text>
			<button @click="lockOrientation('portrait-primary')">竖屏正方</button>
			<button @click="lockOrientation('portrait-secondary')">竖屏反方</button>
			<button @click="lockOrientation('portrait')">竖屏正或反方</button>
		</view>
		<view style="display: flex;  align-items: center;">
			<text style="flex:1;text-align: center;">锁</text>
			<button @click="lockOrientation('landscape-primary')">横屏正方</button>
			<button @click="lockOrientation('landscape-secondary')">横屏反方</button>
			<button @click="lockOrientation('landscape')">横屏正或反方</button>
		</view>
		<button @click="unlockOrientation">解锁屏幕方向</button>
		<text>{{deviceInfo}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: {
					background: '#87ceeb'
				},
				deviceInfo: "",
				CurrentNumber:""
			}
		},
		onLoad() {
		},
		methods: {
			onBeep(){
				plus.device.beep(3);
			},
			onVibrate() {
				plus.device.vibrate(2000);
			},
			async onGetDecive() {
				var str = "";
				str += "设备型号：" + plus.device.model + "\n";
				str += "设备厂商：" + plus.device.vendor + "\n";
				str += "名称：" + plus.os.name + "\n";
				str += "版本：" + plus.os.version + "\n";
				str += "语言：" + plus.os.language + "\n";
				str += "厂商：" + plus.os.vendor + "\n";
				str += "网络类型：";
				var types = {};
				types[plus.networkinfo.CONNECTION_UNKNOW] = "未知"+ "\n";
				types[plus.networkinfo.CONNECTION_NONE] = "未连接网络"+ "\n";
				types[plus.networkinfo.CONNECTION_ETHERNET] = "有线网络"+ "\n";
				types[plus.networkinfo.CONNECTION_WIFI] = "WiFi网络"+ "\n";
				types[plus.networkinfo.CONNECTION_CELL2G] = "2G蜂窝网络"+ "\n";
				types[plus.networkinfo.CONNECTION_CELL3G] = "3G蜂窝网络"+ "\n";
				types[plus.networkinfo.CONNECTION_CELL4G] = "4G蜂窝网络"+ "\n";
				str += types[plus.networkinfo.getCurrentType()];
				let getInfo = function() {
					return new Promise((res, rej) => {
						plus.device.getInfo({
							success: function(e) {
								str += "IMEI：" + e.imei + "\n";
								str += "UUID: " + e.uuid + "\n";
								str += "IMSI：";
								for (let i = 0; i < e.imsi.length; i++) {
									str += e.imsi[i];
								}
								res({
									status: true
								})
							},
							fail: function() { // 授权失败时尝试调用其它API获取
								str += "IMEI：" + plus.device.imei + "\n";
								str += "UUID: " + plus.device.uuid + "\n";
								str += "IMSI：";
								for (let i = 0; i < plus.device.imsi.length; i++) {
									str += plus.device.imsi[i];
								}
								res({
									status: false
								})
							},
							complete: function(e) {
								str += "\n";
								str += "屏幕分辨率：" + plus.screen.resolutionWidth * plus.screen.scale + " x " + plus.screen.resolutionHeight *
									plus.screen.scale + "\n";
								str += "DPI：" + plus.screen.dpiX + " x " + plus.screen.dpiY + "\n";
								str += "屏幕宽高物理分辨率" + plus.screen.height + " ~ " + plus.screen.width + "\n";
								str += "屏幕宽高物理逻辑分辨率" + plus.screen.resolutionHeight + " ~ " + plus.screen.resolutionWidth + "\n";
								str += "逻辑分辨率与物理分辨率的缩放比例:" + plus.screen.scale+ "\n";
								str += "APPID:" + plus.runtime.appid+ "\n";
							}
						});
					})
				}
				let {status} = await getInfo()
				this.deviceInfo = status ? str : this.deviceInfo = '未授予权限！'
			},
			async onGetVolume(){
				let num = await plus.device.getVolume()
				this.CurrentNumber = num
				this.deviceInfo = `
					0表示静音，1表示最大音量值。
					当前音量：${this.CurrentNumber}
				` 
			},
			SetVolume(e){
				e === 'add' ? plus.device.setVolume(this.CurrentNumber+1/10):plus.device.setVolume(this.CurrentNumber-1/10);
				this.onGetVolume()
			},
			async getBrightness(){
				let num = await plus.screen.getBrightness();
				this.CurrentNumber = num
				this.deviceInfo = `
					0表示最低亮度值，1表示最高亮度值。
					当前亮度：${this.CurrentNumber}
				` 
			},
			SetBrightness(e){
				e === 'add' ? plus.screen.setBrightness(this.CurrentNumber+1/10>1?1:this.CurrentNumber+1/10)
					:plus.screen.setBrightness(this.CurrentNumber-1/10<0?0:this.CurrentNumber-1/10);
				this.getBrightness()
			},
			lockOrientation(e){
				plus.screen.lockOrientation(e);
			},
			unlockOrientation(){
				plus.screen.unlockOrientation();
			}
		}
	}
</script>

<style>

</style>
